package com.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;

public class TestNewCachePool {

	static volatile boolean status = true;

	
	public static void main(String[] args) throws InterruptedException {
		
		   ExecutorService pool =Executors.newCachedThreadPool();
		   
		final  SynchronousQueue<Integer> synchronousQueue = new SynchronousQueue<Integer>();
		
//		   for(int i=0;i<1;i++){
			   
			   Runnable ran = new Runnable() {
				@Override
				public void run() {
					 try {

//						 往queue放进去一个element以后就一直wait直到有其他thread进来把这个element取走。

						synchronousQueue.put(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("完成"+Thread.currentThread().getId());
				}
			};
			   
			pool.execute(ran);
			
//		   }
		 

		  
		   
		  
		   
		   Runnable ran1 = new Runnable() {
					@Override
					public void run() {
						
							try {
//								take() 取出并且remove掉queue里的element，取不到东西他会一直等。

								System.out.println("take:"+synchronousQueue.take());
								
								
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						
						
						
					}
				};
				   
		pool.execute(ran1);
		   
		   
		  

		  
//		   status = false;
		   
		   pool.shutdown();
	}

}
